package com.mabaijing.id2;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Scanner;

public class Domo7 {
    public static void main111(String[] args) throws Exception {
        try (InputStream is = new FileInputStream("./hello.txt")) {
            byte[] buf = new byte[1024];
            int n = is.read(buf);
//            System.out.println(n);
//            for (int i = 0; i < n; i++) {
//                System.out.printf("%02x ", buf[i]);
//            }
            String s = new String(buf, 0, n, "UTF-8");//正常读写
            String s1 = new String(buf, 0, n, "GBK");//错误读写
            System.out.println(s);
            System.out.println("--------------");
            System.out.println(s1);
        }
    }

    public static void main(String[] args) throws Exception {
        try (InputStream is = new FileInputStream("./hello.txt")) {
            try (Scanner scanner = new Scanner(is, "UTF-8")) {
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();   // 默认去掉了换行符
                    System.out.println("|" + line + "|");
                }
            }
        }
    }
}
